package be.ac.ua.pats.thesisrobotica.robot.benchmark;

//~--- non-JDK imports --------------------------------------------------------

import be.ac.ua.pats.thesisrobotica.robot.RR.Reflective;
import be.ac.ua.pats.thesisrobotica.robot.communication.Sendable;
import be.ac.ua.pats.thesisrobotica.robot.RR.sensors.Sensor;
import be.ac.ua.pats.thesisrobotica.robot.exceptions.InitializationException;
import be.ac.ua.pats.thesisrobotica.robot.utils.Coordinate;

import lejos.nxt.*;
import lejos.nxt.comm.RConsole;

//~--- JDK imports ------------------------------------------------------------

import java.util.Vector;

/**
 *
 * @author Admin
 */
public class PowerUsage extends Sendable {
    Vector resultVector;    /* Array consisting of the coordinates */
    Sensor sensor;

    /**
     *
     * @param connection
     * @param port
     */
    public PowerUsage(Sensor sensor) {
        super();

        this.sensor  = sensor;
        resultVector = new Vector();
    }

    /**
     *
     */
    public void startTest(int sampleSize, int interval) throws InterruptedException, InitializationException {
        for (int i = 0; i < sampleSize; i++) {
            ((Reflective) sensor).getValue();
            Thread.sleep(interval);
            resultVector.addElement(new Coordinate(Battery.getVoltageMilliVolt(), i * interval));
        }
    }

    public String encapsulate() {
        RConsole.println("starting to xml");

        StringBuffer xml = new StringBuffer();

        xml.append("<?xml version=\"1.0\"?><Benchmark><Robot id=\"" + lejos.nxt.comm.Bluetooth.getAddress()
                   + "\"><Sensor type=\"" + sensor.getType() + "\">");
        RConsole.println("1");

        for (int i = 0; i < resultVector.size(); i++) {
            Coordinate c = (Coordinate) resultVector.elementAt(i);

            xml.append("<Value><x>" + c.getX() + "</x><y>" + c.getY() + "</y></Value>");
        }

        resultVector.clear();

        RConsole.println("2");
        xml.append("</Sensor></Robot></Benchmark>");
        RConsole.println("3");

        return xml.toString();
    }
}
